package com.usian.skill.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.usian.skill.common.CodeMsg;
import com.usian.skill.common.Result;
import com.usian.skill.pojo.SkillUser;
import com.usian.skill.service.GoodsService;
import com.usian.skill.service.SkillUserService;
import com.usian.skill.vo.GoodsDetailVo;
import com.usian.skill.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

@RestController
@RequestMapping("/goods")
public class GoodsController {

	@Autowired
	SkillUserService userService;
	
	/*@Autowired
	RedisService redisService;*/

	@Autowired
	GoodsService goodsService;
	
	@Autowired
	ThymeleafViewResolver thymeleafViewResolver;
	
	@Autowired
	ApplicationContext applicationContext;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;


	
	/**
	 * QPS:1267 load:15 mysql
	 * 5000 * 10
	 * QPS:2884, load:5 
	 * */
    @RequestMapping(value="/to_list",produces = "text/html")
    public String list(HttpServletRequest request, HttpServletResponse response, Model model, SkillUser user) {
		String goodsList = stringRedisTemplate.opsForValue().get("goodsList");
		if (goodsList!=null){
    		return goodsList;
		}
    	List<GoodsVo> list=goodsService.qclist();
		model.addAttribute("goodsList",list);
		WebContext ctx=new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
		String s=thymeleafViewResolver.getTemplateEngine().process("goods_list",ctx);
		stringRedisTemplate.opsForValue().set("goodsList",s,60);
		return s;
    }
    
    @RequestMapping(value="/to_detail2/{goodsId}")
    public String detail2(HttpServletRequest request, HttpServletResponse response, Model model,SkillUser user,
    		@PathVariable("goodsId")long goodsId) {
		System.out.println(goodsId);
			return null;
    }
    
    @RequestMapping(value="/detail/{goodsId}")
	@ResponseBody
    public Result<GoodsDetailVo>  detail(HttpServletRequest request, HttpSession session, HttpServletResponse response, Model model, SkillUser user,
										@PathVariable("goodsId")long goodsId) {
		String us=(String)session.getAttribute("userId");
		String id=session.getId();
		SkillUser skillUser=new SkillUser();
		if (skillUser==null){
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		skillUser.setId(Long.parseLong(us));
		GoodsVo vo=goodsService.getgoodsvobyid(goodsId);
		//将实际转换成毫秒值

		long startAt = vo.getStartDate().getTime();
		long endAt = vo.getEndDate().getTime();
		//获取系统的当前时间
		long now = System.currentTimeMillis();
		int miaoshaStatus = 0;
		int remainSeconds = 0;
		if(now < startAt ) {//秒杀还没开始，倒计时
			miaoshaStatus = 0;
			remainSeconds = (int)((startAt - now )/1000);
		}else  if(now > endAt){//秒杀已经结束
			miaoshaStatus = 2;
			remainSeconds = -1;
		}else {//秒杀进行中
			miaoshaStatus = 1;
			remainSeconds = 0;
		}
//		model.addAttribute("goods",vo);
//		model.addAttribute("user",us);
//		model.addAttribute("miaoshaStatus",miaoshaStatus);
//		model.addAttribute("remainSeconds",remainSeconds);
		GoodsDetailVo goodsDetailVo=new GoodsDetailVo();
		goodsDetailVo.setUser(skillUser);
		goodsDetailVo.setRemainSeconds(remainSeconds);
		goodsDetailVo.setMiaoshaStatus(miaoshaStatus);
		goodsDetailVo.setGoods(vo);
    	return Result.success(goodsDetailVo);

    }

    
}
